<?php
/*
 * index.action.php     Zhuayi 会员模块
 *
 * @copyright    (C) 2005 - 2010  zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-28
 * @author       zhuayi
 * @QQ			 2179942
 */
class member_action extends zhuayi
{
	/* 构造函数 */
	function __construct()
	{
		parent::__construct();
		
		/* 验证登录 */
		$this->user = $this->verify();
		
		$this->db = parent::db();
		
		$this->account = cookie::ret_cookie('account');
		
		
		//echo $_SERVER['HTTP_HOST'];
	}
	
	
	/* 登录页 */
	function login()
	{
		require $this->load_tpl('member_login');
	}
	
	
	/* 登录验证 */
	function login_info()
	{
		$reset  = member_modle::member(array('username'=>$_POST['username'],'password'=>strings::mymd5($_POST['password'])));
		if (empty($reset))
		{
			exit(message::ret_json('-1','帐号密码错误...'));
		}
		/* 写入cookie */
		cookie::set_cookie('user',$reset);
		
		$account = account_action::account($reset['default']);
		
		/* 把默认帐号写入到cookie */
		cookie::set_cookie('account', $account);
		
		exit(message::ret_json('1',''));
	}
	
	/* 引导 */
	function guide($act)
	{
		if (empty($act))
		{
			header("Location:/member/");
		}
		
		
		if ($act == 3)
		{
			//$user = cookie::ret_cookie('user');
			
			/* 载入帐号列表 */
			$account_list = account_modle::account_list(array('mid'=>$this->user['id']));
			
			/* 载入帐号配置文件 */
			$account = $this->load_config('account_action');
			//$maxnum = $account['maxnum'] - 1;
			
			$list = api_modle::api_list();
		}
		
		if ($act == 4)
		{
			
			$user = member_modle::member(array('id'=>$this->user['id']));
			cookie::set_cookie('user',$user);
			
			$account = account_action::account($user['default']);
		
			/* 把默认帐号写入到cookie */
			cookie::set_cookie('account', $account);
			
			if ($user['type'] == '2')
			{
				$info = member_modle::company(array('aid'=>$user['id']));
			}
			else
			{
				$info = member_modle::personal(array('aid'=>$user['id']));
			}
			
			/* 载入默认帐号 */
			$account = account_modle::account(array('a.id'=>$user['default']));
		}
		
		require $this->load_tpl('member_guide'.$act);
	}
	
	/* 引导入库 */
	function guide_info()
	{
		$user = cookie::ret_cookie('user');

		if ($_POST['act'] == '2')
		{
			if ($_POST['type'] == '2')
			{
				/* 企业用户资料入库 */
				$_POST['company']['aid'] = $user['id'];
				member_modle::company_info($_POST['company']);
			}
			else
			{
				/* 个人用户资料入库 */
				$_POST['personal']['aid'] = $user['id'];
				member_modle::personal_info($_POST['personal']);
			}
			
			$array['type'] = $_POST['type'];
			
			$reset = message::ret_json('1','ok');
		}
		elseif ($_POST['act'] == '3')
		{
			$array['default'] = $_POST['default'][0];
			
			$reset = message::ret_json('1','/member/guide/4');
		}
		
		elseif ($_POST['act'] == '4')
		{
			$reset = message::ret_json('1','/member');
		}
		
		$array['setup'] = $_POST['act'];
		/* 更新会员步助 */
		member_modle::member_update($array,' id = '.$user['id']);
		echo $reset;
	}
	
	function guide_verify()
	{
		$user = cookie::ret_cookie('user');
		$user = member_modle::member(array('id'=>$user['id']));
		
		if ($user['setup'] < 4)
		{
			header("Location:/member/guide/".($user['setup']+1));
		}
	}
	/* 找回密码 */
	function forget()
	{
		require $this->load_tpl('member_forget');
	}
	
	/* 找回密码，发送邮件 */
	function forget_info()
	{
		
		/* 先查找会员是否存在 */
		$member = member_modle::member(array('username'=>$_POST['username']));
		if (empty($member))
		{
			exit(message::ret_json('-1','不存在此帐号...'));
		}
		
		$emaildata['username'] = $member['username'];
		$emaildata['webname'] = $this->webname;
		$emaildata['weburl'] = $this->url;
		$emaildata['rand'] = mt_rand();
		$emaildata['ip'] = '127.0.0.1';
		
		/* 把随机数写入表中 */
		member_modle::member_update(array('rand'=>$emaildata['rand']),"username='".$_POST['username']."'");
	
		/* 开始发送邮件 */
		//$reset = $this->email($member['username'],'['.$this->webname.'] 取回密码说明',array(ZHUAYI_ROOT.'/zhuayi/member/template/mail/forget.html'=>$emaildata));
		
		$reset = 'ok';
		
		if (!$reset == 'ok')
		{
			exit(message::ret_json('-1',$reset));
		}
		
		/* 获取邮件后缀 */
		$val = 'http://mail.'.email_action::email_url($member['username']);
		
		echo message::ret_json('1',$val);
	}
	
	/* 找回密码验证 */
	function verifys($username,$rand)
	{
		/* 查找会员 */
		$member = member_modle::member(array('username'=>$username,'rand'=>$rand));
		if (empty($member))
		{
			message::showmsg('错误的来源地址',$this->url.'login');
		}
		
		require $this->load_tpl('member_verifys');
	}
	
	/* 找回密码验证提交 */
	function verifys_info()
	{
		/* 验证两次密码是否一样 */
		if (empty($_POST['password']) || strlen($_POST['password']) < 6 || $_POST['password']!=$_POST['password2'])
		{
			exit(message::ret_json('-1','两次密码输入的不一样'));
		}
		
		/* 查找会员 */
		$member = member_modle::member(array('username'=>$_POST['username'],'rand'=>$_POST['rand']));
		if (empty($member))
		{
			exit(message::ret_json('-1','非法提交'));
		}
		
		/* 更新密码 */
		member_modle::member_update(array('password'=>strings::mymd5($_POST['password']),'rand'=>''),"id='".$member['id']."'");
		
		exit(message::ret_json('1',$this->url.'login'));
	}
	
	/* 注册 */
	function reg()
	{
		require $this->load_tpl('member_reg');
	}
	
	/* 会员入库 */
	function reg_info()
	{
		/* 验证帐号是否邮箱 */
		if (!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+", $_POST['username']))
		{
			exit(message::ret_json('-1','帐号必须为邮箱...'));
		}
		
		/* 验证两次密码是否一样 */
		if (empty($_POST['password']) || strlen($_POST['password']) < 6 || $_POST['password']!=$_POST['password2'])
		{
			exit(message::ret_json('-1','两次密码输入的不一样'));
		}
		
		if (empty($_POST['terms']))
		{
			exit(message::ret_json('-1','请同意我们的服务条款'));
		}
		
		
		/* 查询是否有数据 */
		$reset  = member_modle::member(array('username'=>$_POST['username']));
		if ($reset)
		{
			exit(message::ret_json('-1','已经存在此帐号'));
		}
		
		/* 发送邮件 */
		$emaildata['username'] = $_POST['username'];
		$emaildata['webname'] = $this->webname;
		$emaildata['weburl'] = $this->url;
		$emaildata['rand'] = mt_rand();
		$emaildata['ip'] = '127.0.0.1';
		
	
		/* 开始发送邮件 */
		$this->email($_POST['username'],'['.$this->webname.'] : 请完成您的帐号申请',array(ZHUAYI_ROOT.'/zhuayi/member/template/mail/reg.html'=>$emaildata));
		
		/* 写入数据 */
		$_POST['activation'] = $emaildata['rand'];
		$_POST['dtime'] = time();
		$_POST['password'] = strings::mymd5($_POST['password']);
		
		$_POST['id'] = member_modle::member_info($_POST);
		
		/* 获取邮件后缀 */
		$val = 'http://mail.'.email_action::email_url($_POST['username']);
		
		exit(message::ret_json('1',$val));
	}
	
	/* 激活帐号 */
	function activation($username,$activation)
	{
		/* 查找会员 */
		$member = member_modle::member(array('username'=>$username,'activation'=>$activation));
		if (empty($member))
		{
			message::showmsg('错误的来源地址',$this->url.'login');
		}
		
		member_modle::member_update(array('activation'=>''),"id='".$member['id']."'");
		
		cookie::set_cookie('user',$member);
		
		message::showmsg('帐号激活成功！',$this->url.'');
	}
	
	
	/* 退出登录 */
	function logout()
	{
		/* 清除cookie */
		cookie::set_cookie('user','');

	}
	
	/*  验证是否登录 */
	function verify()
	{
		
		$user = cookie::ret_cookie('user');
		
		$url = $this->routing['routing']['m'].'_'.$this->routing['routing']['c'];
		if ($url == 'member_login' || $url == 'member_login_info' || $url == 'member_reg'|| $url == 'member_reg_info' || $url == 'member_forget'|| $url == 'member_forget_info' || $url == 'member_verifys' || $url == 'member_verifys_info' || $url == 'member_activation')
		{
			return $user;
		}
		
		if (empty($user))
		{
			message::showmsg('您还没有登录...','/member/login');
		}
		else
		{
			return $user;
		}
	}
	
	/* 会员首页 */
	function index($feature='')
	{
		/* 验证会员注册步助是否完成 */
		$this->guide_verify();
		require $this->load_tpl('member_index');
	}
	
	/* 会员右侧 */
	function right($c='index')
	{

		require $this->load_tpl('member_right');
	}
	
	/* 会员面板头部 */
	function hread()
	{
		$this->webname .= " - 会员面板";
		
		$user = cookie::ret_cookie('user');
		
		require $this->load_tpl('member_hread');
	}
	
	/* 切换帐号 */
	function cutover()
	{

		/* 查询全部帐号 */
		$list = account_modle::account_list(array('mid'=>$this->user['id']));
		require $this->load_tpl('member_cutover');
	}
	
	/* 切换帐号 */
	function cutover_info($id)
	{
		$account = account_modle::account(array('a.mid'=>$this->user['id'],'a.id'=>$id));
		if (empty($account['id']))
		{
			message::showmsg('您来错地方了...','/member/login');
		}
		else
		{
			cookie::set_cookie('account',$account);
		}
		header("Location:".$_SERVER["HTTP_REFERER"]);

	}
	
	
	/* 会员面板底部 */
	function foot()
	{
		require $this->load_tpl('member_foot');
	}
	
	/* @提到我的 */
	function atme()
	{
		$c = 'atme';
		
		require $this->load_tpl('member_atme');
	}
	
	/* 我的评论 */
	function comment($act = 'comments_to_me')
	{
		$c = 'comment';
		
		require $this->load_tpl('member_comment');
	}
	
	/* 我的收藏 */
	function favs()
	{
		$c = 'favs';
		require $this->load_tpl('member_favs');
	}
	
	/* 我的微博 */
	function my()
	{
		require $this->load_tpl('member_my');
	}
}
?>